package com.vilicode.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPObject;
import com.vilicode.bean.Book;
import com.vilicode.bean.BookType;
import com.vilicode.service.BookService;
import com.vilicode.service.BookTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
public class IndexController {

    @Autowired
    public BookService bookService;

    @Autowired
    public BookTypeService bookTypeService;

    @RequestMapping("/index.action")
    public String Init(HttpServletRequest request)
    {
        log.info("/index.action begin");
        List<Book> books=bookService.queryBookByRecommendType(1,1,6);
        if(books.size()>0){
            log.info("scrollBook:{}", JSON.toJSONString(books.get(0)));
            request.setAttribute("scrollBook",books.get(0));
        }
        books=bookService.queryBookByRecommendType(2,1,6);
        request.setAttribute("hotList",books);
        books=bookService.queryBookByRecommendType(3,1,8);
        request.setAttribute("newList",books);
        List<BookType> bookTypes=bookTypeService.queryBookTypes();
        request.getServletContext().setAttribute("bookTypes",bookTypes);
/*
        作用范围：
        request.setAttribute：请求范围，仅在当前请求中有效。
        request.getServletContext().setAttribute：应用范围，整个应用中有效。
        生命周期：
        request.setAttribute：从设置属性开始，直到请求结束。
        request.getServletContext().setAttribute：从设置属性开始，直到应用服务器重启或上下文被销毁。
        适用场景：
        request.setAttribute：适用于在一个请求处理过程中传递数据。
        request.getServletContext().setAttribute：适用于在整个应用中共享数据。*/
        log.info("/index.action end");
        return "index";
    }
}
